package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Warehouse;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.WarehouseService;
import com.mrtan.vo.WarehouseMonthCountVO;
import com.mrtan.vo.WarehouseVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 方雨淳
 * @version 2023.1
 * @date 2023/12/12 15:36
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    private WarehouseService warehouseService;

    @PostMapping("/allwarehouse")
    public SaResult allwarehouse(@RequestBody WarehouseVO warehouseVO) {
        PageHelper.startPage(warehouseVO.getPage(), warehouseVO.getLimit());
        List<Warehouse> warehouses = warehouseService.selectAll(warehouseVO);

        PageInfo<Warehouse> pageInfo = new PageInfo<>(warehouses);
        PageBeanDto<Warehouse> pageBeanDto = new PageBeanDto<>();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    @PostMapping("/updateStatus")
    public SaResult updateStatus(@RequestBody Warehouse warehouse) {
        int i = warehouseService.updateWarehouse(warehouse);
        if (i > 0) {
            return SaResult.ok();
        }
        return SaResult.error("修改失败");
    }

    @GetMapping("/addwarehouse")
    public SaResult addwarehouse(@RequestBody Warehouse warehouse) {
        System.out.println(warehouse);
        int i = warehouseService.addNewWarehouse(warehouse);
        if (i > 0) {
            return SaResult.ok();
        }
        return SaResult.error("添加仓库失败");
    }

    @PostMapping("/warehouses")
    public SaResult allwarehouse() {
        List<Warehouse> warehouses = warehouseService.selectAll(null);
        return SaResult.ok().setData(warehouses);
    }

    @GetMapping("/getName")
    public SaResult getWarehouseNames(Integer id){
        System.out.println(id);
        if (id == null){
            List<String> allWarehouseNames = warehouseService.getAllWarehouseNames();
            return SaResult.ok().setData(allWarehouseNames);
        }
        String warehouseNames = warehouseService.getWarehouseNames(id);
        return SaResult.ok().setData(warehouseNames);
    }
    /**
     *查询销售订单数量
     */

    @GetMapping("/getTotalCount")
    public SaResult getTotalCount(WarehouseMonthCountVO warehouseMonthCountVO){
        System.out.println(warehouseMonthCountVO);
        Integer totalCount = warehouseService.getTotalCount(warehouseMonthCountVO);
        return SaResult.ok().setData(totalCount);
    }
    /**
     *产品种类
     */

    @GetMapping("/getProductCount")
    public SaResult getProductCount(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer productCount = warehouseService.getProductCount(warehouseMonthCountVO);
        return SaResult.ok().setData(productCount);
    }
    /**
     *出入库数量的和
     */

    @GetMapping("/getInandOutCount")
    public SaResult getInandOutCount(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer inandOutCount = warehouseService.getInandOutCount(warehouseMonthCountVO);
        if (inandOutCount == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(inandOutCount);
    }

    /**
     * 出入库产品成本价格合计
     */

    @GetMapping("/getTotalCostByProduct")
    public SaResult getTotalCostByProduct(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalCostByProduct = warehouseService.getTotalCostByProduct(warehouseMonthCountVO);
        if (totalCostByProduct == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalCostByProduct);
    }

    @GetMapping("/getTotalCostByStock")
     public SaResult getTotalCostByStock(WarehouseMonthCountVO  warehouseMonthCountVO){
        Integer totalCostByStock = warehouseService.getTotalCostByStock(warehouseMonthCountVO);
        if (totalCostByStock == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalCostByStock);
    }

    @GetMapping("/getTotalQuantityByWarehouse")
    public SaResult getTotalQuantityByWarehouse(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalQuantityByWarehouse = warehouseService.getTotalQuantityByWarehouse(warehouseMonthCountVO);
        if (totalQuantityByWarehouse == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalQuantityByWarehouse);
    }

    @GetMapping("/getTotalQuantityByWarehouseAndDate")
    public SaResult getTotalQuantityByWarehouseAndDate(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalQuantityByWarehouseAndDate = warehouseService.getTotalQuantityByWarehouseAndDate(warehouseMonthCountVO);
        if (totalQuantityByWarehouseAndDate == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalQuantityByWarehouseAndDate);
    }
    @GetMapping("/getTotalCostByWarehouseAndDate")
    public SaResult getTotalCostByWarehouseAndDate(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalCostByWarehouseAndDate = warehouseService.getTotalCostByWarehouseAndDate(warehouseMonthCountVO);
        if (totalCostByWarehouseAndDate == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalCostByWarehouseAndDate);
    }

    @GetMapping("/getTotalCostByWarehouseAndDates")
    public SaResult getTotalCostByWarehouseAndDates(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalCostByWarehouseAndDates = warehouseService.getTotalCostByWarehouseAndDates(warehouseMonthCountVO);
        if (totalCostByWarehouseAndDates == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalCostByWarehouseAndDates);
    }

    @GetMapping("/getTotalRevenueByWarehouseAndDate")
    public SaResult getTotalRevenueByWarehouseAndDate(WarehouseMonthCountVO warehouseMonthCountVO){
        Integer totalRevenueByWarehouseAndDate = warehouseService.getTotalRevenueByWarehouseAndDate(warehouseMonthCountVO);
        if (totalRevenueByWarehouseAndDate == null){
            return SaResult.error().setData(0);
        }
        return SaResult.ok().setData(totalRevenueByWarehouseAndDate);
    }

}
